package cn.edu.nuc.controller;

import cn.edu.nuc.dao.BaseTest;
import cn.edu.nuc.ssm.controller.DemoController;
import cn.edu.nuc.ssm.dto.Entity;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import javax.servlet.ServletContext;
import java.io.IOException;

/**
 * Created by Administrator on 2017/4/1.
 */
@WebAppConfiguration
public class DemoControllerTest_mock extends BaseTest{

    @Autowired
    private DemoController demoController;

    MockMvc mockMvc;

    @Before
    public void setup() {

        mockMvc = MockMvcBuilders.standaloneSetup(demoController).build();
    }

    @Test
    public void getArticleListTest() throws Exception {


        String str = "we";
        //发送请求
        ResultActions resultActions = this.mockMvc.perform(MockMvcRequestBuilders.post("/demo/demo")
                .accept(MediaType.ALL).param("criJson",str));
        MvcResult mvcResult = resultActions.andReturn();
        String result = mvcResult.getResponse().getContentAsString();
        System.out.println("=====客户端获得反馈数据:" + result);

    }
    @Test
    public void getEntity() throws Exception{
        Entity entity = new Entity();
        entity.setMac("sdf");

        ObjectMapper objectMapper = new ObjectMapper();
        String str =  objectMapper.writeValueAsString(entity);
        System.out.println(str);
        String responseString = mockMvc.perform( MockMvcRequestBuilders.post("/demo/json111").contentType(MediaType.APPLICATION_JSON).content(str))
                .andReturn().getResponse().getContentAsString();

        System.out.print(responseString);


//        ResultActions resultActions = this.mockMvc.
//                perform(MockMvcRequestBuilders.post("/demo/json111").accept(MediaType.APPLICATION_JSON).content(str));
//        MvcResult mvcResult =  resultActions.andReturn();
//
//        String result =  mvcResult.getResponse().getContentAsString();
//        System.out.println("====客户端获得反馈数据:" + result);

    }

}
